<?php

namespace Hanson\MyVbot\Handlers\Type;

use Carbon\Carbon;
use Hanson\Vbot\Contact\Friends;
use Hanson\Vbot\Contact\Groups;
use Hanson\Vbot\Message\Text;
use Hanson\Vbot\Message\File;
use Illuminate\Support\Collection;
use Hanson\MyVbot\Amazon\Comment\Comments;

class TextType
{
    public static function messageHandler(Collection $message, Friends $friends, Groups $groups)
    {
        if ($message['type'] === 'text') {
            if ($message['content'] === 'time') {
                $datetime = Carbon::parse(vbot('config')->get('server.time'));
                Text::send($message['from']['UserName'], 'Running:'.$datetime->diffInHours().'小时');
            }

            /*
            if ($message['content'] === '拉我') {
                $username = $groups->getUsernameByNickname('Vbot 体验群');
                $groups->addMember($username, $message['from']['UserName']);
            }

            if ($message['content'] === '叫我') {
                $username = $friends->getUsernameByNickname('HanSon');
                Text::send($username, '主人');
            }

            if ($message['content'] === '报名') {
                $username = $groups->getUsernameByNickname('vbot 反馈群');
                $groups->addMember($username, $message['from']['UserName']);
            }
            */

            if ($message['content'] === '抓评论') {

                try {
                    var_dump($message);
                    $res = Comments::get_comments('');
                }catch (\Exception $e){
                    Text::send($message['from']['UserName'], $e->getMessage());
                    return;
                }

                if(false !== $res){
                    try {
                        //$username = $friends->getUsernameByRemarkName("郭棋", $blur = false);
                        //File::send($username, "/tmp/" . $res);

                        File::send($message['from']['UserName'], "/tmp/" . $res);
                    }catch (\Exception $e){
                        Text::send($message['from']['UserName'], $e->getMessage());
                    }
                }else{
                    Text::send($message['from']['UserName'], ' 抓取失败了！');
                }
            }
        }

    }
}
